package com.wyclabs.cardmaker;
import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;

import cn.hutool.core.io.FileUtil;

/**
 * @author TylerW
 * @date 06/22/21 2:31 PM
 */
public class CommonService {

    public static final Map<String, CommonController> controllers = new HashMap<>();

    public static Properties config;

    public static String selectedCamp = "";

    public static String selectedFolder = "";

    public static File readResource(String path) {
        if (isJar()) {
            return new File(getRealPath() + path);
        } else {
            return new File(Objects.requireNonNull(CardGenerator.class.getClassLoader().getResource(path)).getPath());
        }
    }

    public static InputStream readAsResource(String path) {
        if (isJar()) {
            return FileUtil.getInputStream(getRealPath() + path);
        } else {
            return CommonService.class.getClassLoader().getResourceAsStream(path);
        }
    }

    public static String getRealPath() {
        String path = getJarPath();
        path = path.substring(0, path.lastIndexOf("/")) + "/";
        return path;
    }

    public static boolean isJar() {
        return !(new File(getJarPath()).isDirectory());
    }

    public static String getJarPath() {
        return CommonService.class.getProtectionDomain().getCodeSource().getLocation().getPath();
    }

}
